import { defineConfig, loadEnv } from 'vite';
import path from 'path';
import createVitePlugins from './vite/plugins';

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
    const env = loadEnv(mode, process.cwd());
    const { VITE_APP_ENV } = env;
    
    return {
        // 部署生产环境和开发环境下的URL。
        // 默认情况下，vite 会假设你的应用是被部署在一个域名的根路径上
        // 例如 https://www.xxx.vip/。如果应用被部署在一个子路径上，你就需要用这个选项指定这个子路径。例如，如果你的应用被部署在 https://www.xxx.com/admin/，则设置 baseUrl 为 /admin/。
        base: (VITE_APP_ENV === 'production' || VITE_APP_ENV === 'test') ? '/map' : '/',
        plugins: createVitePlugins(env, command === 'build'),
        resolve: {
            // https://cn.vitejs.dev/config/#resolve-alias
            alias: {
                // 设置路径
                '~': path.resolve(__dirname, './'),
                // 设置别名
                '@': path.resolve(__dirname, './src')
            },
            // https://cn.vitejs.dev/config/#resolve-extensions
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
            dedupe: ['vue']
        },

        // vite 相关配置
        server: {
            port: 80,
            host: true,
            open: false,
            proxy: {
                '/app' : {
                    // target: 'http://172.16.6.101:7100',
                    // target: 'http://172.16.6.14:7101',
                    target: "http://172.16.7.251:7100",
                    changeOrigin: true
                },
                '/tj_app_api' : {
                    // target: 'http://172.16.6.101:7100',
                    target: 'https://t.sczrsz.com',
                    // target: "http://172.16.6.15:7100",
                    changeOrigin: true
                }
            }
        },

        //fix:error:stdin>:7356:1: warning: "@charset" must be the first rule in the file
        css: {
            postcss: {
                plugins: [{
                    postcssPlugin: 'internal:charset-removal',
                    AtRule: {
                        charset: (atRule) => {
                            if (atRule.name === 'charset') {
                                atRule.remove();
                            }
                        }
                    }
                }]
            }
        },

        // 打包优化
        build: {
            chunkSizeWarningLimit: 2000,
            rollupOptions: {
                output: {
                    entryFileNames: 'static/js/[name]-[hash].js',
                    chunkFileNames: 'static/js/[name]-[hash].js',
                    assetFileNames: assetInfo => {
                        const { type, name } = assetInfo;
                        if (type === 'asset' && /\.(jpe?g|png|gif|svg)$/i.test(name)) {
                            if (/\.(jpe?g|png|gif)$/i.test(name)) {
                                return 'static/img/[name].[hash].[ext]';
                            }
                            
                            if (/\.svg$/i.test(name)) {
                                return 'static/svg/[name].[hash].[ext]';
                            }

                            if (/\.(ttf|woff|woff2|eot)$/i.test(name)) {
                                return 'static/fonts/[name].[hash].[ext]';
                            }
                        }

                        return 'static/[ext]/[name]-[hash].[ext]';
                    },

                    manualChunks(id) {
                        if (id.includes('node_modules')) {
                            if (id.includes('zrender')) {
                                return 'zRender';
                            } else if (id.includes('echarts')) {
                                return 'echarts';
                            } else if (id.includes('pinyin-pro')) {
                                return 'pinyin-pro';
                            } else if (id.includes('element-plus')) {
                                return 'el-plus';
                            } else if (id.includes('pdf-vue3')) {
                                return 'pdf-vue3';
                            } else {
                                return 'vendor';
                            }                                                       
                        }
                    }
                }
            },
            minify: 'terser',
            terserOptions: {
                compress: {
                    drop_console: true,
                    drop_debugger: true
                }
            }
        }
    }
});